package com.cn.study.service.deviceimpl;

import com.alibaba.fastjson.JSON;
import com.cn.study.config.ProducerConfig;
import com.cn.study.service.Send;
import com.cn.study.service.SendMessageDeviceService;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author mjy
 * @date 2021/12/15
 * @description
 */
public class SendMessageDeviceServiceImpl extends Send implements SendMessageDeviceService {


    private Logger log = LoggerFactory.getLogger(SendMessageDeviceServiceImpl.class);

    @Autowired
    ProducerConfig producerConfig;

    @Autowired
    private DefaultMQProducer defaultMQProducer;








    @Override
    public void send(String topic, String tag, String msg) throws RemotingException, MQClientException, InterruptedException {
        /**
         * 设置消息
         */
        Message message = new Message(topic, tag, msg.getBytes());
        defaultMQProducer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("rocketMq消息发送成功"+ JSON.toJSONString(sendResult));
            }

            @Override
            public void onException(Throwable e) {
                log.info("rocketMq消息发送失败"+ e);
            }
        });
    }

}
